package me.lc.hammerheads.sprite;

import java.util.ArrayList;
import java.util.List;

import me.lc.hammerheads.core.GameSystem;

import com.wiyun.engine.actions.Action.Callback;
import com.wiyun.engine.actions.FadeTo;
import com.wiyun.engine.actions.IntervalAction;
import com.wiyun.engine.actions.MoveTo;
import com.wiyun.engine.nodes.Node;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.utils.ResolutionIndependent;

/**
 * 血
 * 
 * @author Administrator
 * 
 */
public class HeartBar extends Node {
	Sprite heartBg;
	List<Sprite> hearts;
	int lastHeartIndex = 4;
	int lastHeartCount = 5;

	public HeartBar() {
		heartBg = Sprite.make(GameSystem.tex_heartBg);
		this.addChild(heartBg);
		arrangeHeart();
	}

	/**
	 * 安排heart的位置
	 */
	private void arrangeHeart() {
		hearts = new ArrayList<Sprite>();
		float startX = (GameSystem.tex_heartBg.getWidth() - (GameSystem.tex_heart
				.getWidth() * 5.0f) / 2.0f)
				- GameSystem.tex_heartBg.getWidth()
				+ (GameSystem.tex_heart.getWidth() / 2.0f);
		for (int i = 0; i < 5; i++) {
			Sprite heart = Sprite.make(GameSystem.tex_heart);
			hearts.add(heart);
			heart.setPosition(startX + i * GameSystem.tex_heart.getWidth(), 0);
			this.addChild(heart);
		}
	}

	/**
	 * 更新爱心显示,如果是扣血，先调用爱心消失动画
	 * 
	 * @param heartCount
	 */
	public void updateHeart(int heartCount) {
		if (heartCount < lastHeartCount) {
			missHeartAction();
			lastHeartCount = heartCount;
		}
		for (int i = 0; i < 5; i++) {
			Sprite heart = hearts.get(i);
			if (i < heartCount) {
				heart.setTexture(GameSystem.tex_heart);
			} else {
				heart.setTexture(GameSystem.tex_heart_empty);
			}
		}
		lastHeartIndex = heartCount - 1;
		lastHeartCount = heartCount;
	}

	/**
	 * 爱心消失动画
	 */
	private void missHeartAction() {
		if (lastHeartIndex >= 0 && lastHeartIndex < hearts.size()) {
			Sprite heart = hearts.get(lastHeartIndex);
			float startX = heart.getPositionX();
			float startY = heart.getPositionY();
			float endX = startX + ResolutionIndependent.resolveDp(20);
			float endY = startY - ResolutionIndependent.resolveDp(15);

			Sprite brokenHeart = Sprite.make(GameSystem.tex_brokenheart);
			brokenHeart.autoRelease(true);
			brokenHeart.setPosition(startX, startY);
			this.addChild(brokenHeart);

			IntervalAction move = (IntervalAction) MoveTo.make(0.6f, startX,
					startY, endX, endY).autoRelease();
			brokenHeart.runAction(move);

			IntervalAction fade = (IntervalAction) FadeTo.make(0.6f, 255, 50)
					.autoRelease();
			fade.setCallback(new BrokenHeartActionCallback(brokenHeart));
			brokenHeart.runAction(fade);
		}
	}

	class BrokenHeartActionCallback implements Callback {

		Sprite brokenHeart;

		public BrokenHeartActionCallback(Sprite brokenHeart) {
			this.brokenHeart = brokenHeart;

		}

		@Override
		public void onStart(int arg0) {
		}

		@Override
		public void onStop(int arg0) {
			HeartBar.this.removeChild(brokenHeart, true);
		}

		@Override
		public void onUpdate(int arg0, float arg1) {

		}

	}
}
